package com.weiyi.mashangzhu.Roomer.Activity;

import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Base.BaseActivity;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Utils.LoadingDialog;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class IdentityActivity extends BaseActivity {


    @BindView(R.id.identity_name)
    EditText identityName;
    @BindView(R.id.identity_number)
    EditText identityNumber;
    @BindView(R.id.identity_sex)
    TextView identitySex;
    @BindView(R.id.identity_root)
    LinearLayout identityRoot;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_number)
    TextView tvNumber;
    @BindView(R.id.tv_sex)
    TextView tvSex;
    @BindView(R.id.identity_confirm)
    Button identityConfirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_identity);
        ButterKnife.bind(this);
        OkHttpUtils.get().addParams("token", SPUtils.get(this, "token", "") + "")
                .url(API.BaseApi_R + API.CheckIdCard_Api)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        T.showShort(IdentityActivity.this, "数据获取失败，请稍后再试");
                    }

                    @Override
                    public void onResponse(String response) {
                        Log.e("999999999999999999999", "=" + response);
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            JSONObject jsonObject1 = jsonObject.getJSONObject("data");
                            if (jsonObject1.getInt("uCheckid") == 0) {
                                //没进行过身份验证
                                identityName.setVisibility(View.VISIBLE);
                                identityNumber.setVisibility(View.VISIBLE);
                                identitySex.setVisibility(View.VISIBLE);
                                identityConfirm.setVisibility(View.VISIBLE);
                                tvName.setVisibility(View.GONE);
                                tvNumber.setVisibility(View.GONE);
                                tvSex.setVisibility(View.GONE);

                            } else {
                                //已进行过身份验证
                                identityName.setVisibility(View.GONE);
                                identityNumber.setVisibility(View.GONE);
                                identitySex.setVisibility(View.GONE);
                                identityConfirm.setVisibility(View.GONE);
                                tvName.setVisibility(View.VISIBLE);
                                tvNumber.setVisibility(View.VISIBLE);
                                tvSex.setVisibility(View.VISIBLE);
                                tvName.setText(jsonObject1.getString("realName") + "");
                                String code = jsonObject1.getString("cardCode") + "";
                                if (code!=null&&!code.equals("")){
                                    tvNumber.setText(code.substring(0, 6) + "********" + code.substring(14));
                                }

                                if (jsonObject1.getInt("uSex") == 1) {
                                    tvSex.setText("男");
                                } else if (jsonObject1.getInt("uSex") == 0) {
                                    tvSex.setText("女");
                                }

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });
    }

    @Override
    public void initView() {
        super.initView();
    }

    private void sex() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view = View.inflate(this, R.layout.dialog_sex_select, null);
        final TextView men = (TextView) view.findViewById(R.id.man);
        final TextView women = (TextView) view.findViewById(R.id.women);
        builder.setView(view);
        final AlertDialog dialog = builder.show();
        men.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                identitySex.setText(men.getText() + "");
            }
        });
        women.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                identitySex.setText(women.getText() + "");
            }
        });
    }

    @OnClick({R.id.back, R.id.identity_sex, R.id.identity_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.identity_sex:
                sex();
                break;
            case R.id.identity_confirm:
                if ((identityName.getText() + "").equals("")) {
                    T.showShort(this, "请输入真实姓名");
                } else {
                    if ((identityNumber.getText() + "").equals("")) {
                        T.showShort(this, "请输入身份证号");
                    } else {
                        if ((identitySex.getText() + "").equals("")) {
                            T.showShort(this, "请选择性别");
                        } else {
                            String sex = "";
                            if ((identitySex.getText() + "").equals("男")) {
                                sex = "1";
                            } else {
                                sex = "0";
                            }
                            LoadingDialog.show(this);
                            OkHttpUtils.get()
                                    .addParams("token", SPUtils.get(this, "token", "") + "")
                                    .addParams("hRealname", identityName.getText() + "")
                                    .addParams("hIdcardnum",identityNumber.getText()+"")
                                    .addParams("uSex",sex)
                                    .url(API.BaseApi_R+API.CheckIdCard_Api_L)
                                    .build()
                                    .execute(new StringCallback() {
                                        @Override
                                        public void onError(Request request, Exception e) {
                                            LoadingDialog.dismiss(IdentityActivity.this);
                                            T.showShort(IdentityActivity.this,"数据获取失败，请稍后再试");

                                        }

                                        @Override
                                        public void onResponse(String response) {
                                            LoadingDialog.dismiss(IdentityActivity.this);
                                            Log.e("77777777777777","="+response);
                                            if (response!=null){
                                                try {
                                                    JSONObject jsonObject=new JSONObject(response);
                                                    if (jsonObject.getInt("status")==0){
                                                        finish();
                                                    }
                                                    T.showShort(IdentityActivity.this,jsonObject.getString("msg"));
                                                } catch (JSONException e) {
                                                    e.printStackTrace();
                                                }
                                            }
                                        }
                                    });

                        }
                    }
                }


                break;
        }
    }
}
